import { AutoTradeType } from "../../trader/types";
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import StrategyEntity from "../strategy/strategy.entity";
import TradeAccountEntity from "../trade-account/TradeAccount.entity";


export interface SymbolOrderMoreConfig {
    stopLoss: number;
    stopProfit: number;
    /** 贪婪 */
    greedy: boolean;
}
@Entity()
export default class SymbolOrderConfigEntity{
    @PrimaryGeneratedColumn()
    public id!: number;

    @Column()
    public userid!: number;

    @Column({type: 'varchar', length: 16})
    public symbol!: string;

    // @Column()
    // public trade_ccount_id: number;

    // public trade_ccount?: TradeAccountEntity;

    /** 买入花费的u */
    @Column({type: 'float', comment: "买入花费的u", default: 0})
    public buy_usdt!: number;
    /** 买入花费的u */
    @Column({type: 'float', comment: "卖出花费的u", default: 0})
    public sell_usdt!: number;
    /** 买入开多 */
    @Column({type: 'float', comment: "开多", default: 0})
    public open_long!: number;

    /** 开空 */
    @Column({type: 'float', comment: "开空", default: 0})
    public open_short!: number;

    /** 平多 */
    @Column({type: 'float', comment: "平多", default: 0})
    
    public close_long!: number;
    
    /** 平空 */
    @Column({type: 'float', comment: "平空", default: 0})
    public close_short!: number;

    @Column({type: 'tinyint', default: false})
    public enabled!: boolean;

    @Column({length: 16})
    public order_type: "MARKET" | "LIMIT";
    /**
     * 下单类型
     */
    @Column({length: 16})
    public trade_type: AutoTradeType;

    @Column({
        default: 1
    })
    public lever_rate!: number;

    @ManyToOne(() => StrategyEntity)
    @JoinColumn({ name: 'strategy_id' })
    public strategy?: StrategyEntity;

    public strategy_id: number;
    
    @Column({
        type: 'text',
        transformer: { 
            from(value: string) { 
                return value.startsWith('{') ? JSON.parse(value) : {}
            }, 
            to(value: SymbolOrderMoreConfig) { 
                return JSON.stringify(value) 
             } 
        }
    })
    public config: SymbolOrderMoreConfig;
}
